﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.BussLogic;
using System.Data;
using TimKen.DataEntity;
using System.Data.SqlClient;
using TimKen.DataAccess;
using System.Globalization;
using System.Threading;
using System.Text;

namespace TimKen.Web
{
    public partial class Index : TimKen.App.WebPage
    {
        #region 属性

        public string EntityContent;
        public string BanProject;
        public string BanPP;
        /// <summary>
        /// 多语言编号
        /// </summary>
        public int LanguageID
        {
            get
            {
                switch (Thread.CurrentThread.CurrentUICulture.ToString())
                {
                    case "zh-CN":
                        return 1;
                    case "en-US":
                        return 2;
                    default:
                        return 1;
                }
            }
        }

        #endregion
                       
        protected void btnChinese_Click(object sender, EventArgs e)
        {                              
            SetLanguage("zh-CN");
        }

        protected void btnEnglish_Click(object sender, EventArgs e)
        {
            SetLanguage("en-US");
        }
        #region 系统事件

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (CurrentUser != null)
            //{
            //    if (!CurrentUser.IsValid)
            //    {
            //        //请激活用户   
            //        Alert(GetLangString("AuthenticationUserTitle"),"/User/CheckUser.aspx");
            //    }
            //}
            if (!IsPostBack)
            {
                InitData();
            }

        }

        #endregion


        #region 自定义方法

        public void InitData()
        {

            //公司新闻
            ArticleManager am = new ArticleManager();
            DataSet dsArticle = am.GetList(5, 1, " TypeID=1 AND LanguageID=" + LanguageID);
            LstCompanyList.Text = GetLstCompanyList(dsArticle);

            //绑定公司介绍
            List<DataEntity.Article> articleList = am.GetModelList("  TypeID=6 AND LanguageID=" + LanguageID);
            DataEntity.Article Info = articleList[0];
            EntityContent = checkStr(Info.Content);
            EntityContent = GetSubString(EntityContent, 87, 180, LanguageID);
            //产品广告
            AdvertisementDAO dao=new AdvertisementDAO ();
            DataSet ds= dao.getProjectList();
            BanProject=getBanTop(ds);
            //品牌广告
            BanPP=getPP(dao.getPPList());
        }
       /// <summary>
       /// 获取产品广告       
       /// </summary>
        public string getBanTop(DataSet ds)
        {
            StringBuilder buf = new StringBuilder();
            StringBuilder buf1 = new StringBuilder();
            string str = "<li><a href=\"{0}\"><img src=\"{1}\" width=\"688\" height=\"243\" alt=\"\" /></a></li>";
            string str1 = "<li>{0}</li>";
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        buf.Append(string.Format(str, dt.Rows[i]["Url"].ToString(), dt.Rows[i]["ImagePath"].ToString()));
                        buf1.Append(string.Format(str1,i+1));
                    }
                }
            }
            ViewState["SmallNum"] = buf1.ToString();
            return buf.ToString();
        }
        /// <summary>
        /// 得到品牌
        /// </summary>
        /// <returns></returns>
        public string getPP(DataSet ds)
        {
            StringBuilder buf = new StringBuilder();
            string str = "<li><a href=\"{0}\"><img src=\"{1}\" width=\"688\" height=\"244\" alt=\"品牌：{2}\" /></a></li>";
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        buf.Append(string.Format(str, dt.Rows[i]["Url"].ToString(), dt.Rows[i]["ImagePath"].ToString(), dt.Rows[i]["pp"].ToString()));
                    }
                }
            }
            return buf.ToString();
        }
        /// <summary>
        /// 获取公司动态新闻列表
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public string GetLstCompanyList(DataSet ds)
        {
            StringBuilder sb = new StringBuilder();
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0] as DataTable;
                if (dt != null && dt.Rows.Count > 0)
                {
                    sb.Append("<ul>");
                    foreach (DataRow dr in dt.Rows)
                    {
                        string Title = Convert.ToString(dr["Title"]);
                        sb.AppendFormat("<li><a href=\"/NewsDetail.aspx?ArticleID={1}\">{0}</a></li>", GetSubString(Title, 22, 35, LanguageID), Convert.ToString(dr["id"]));
                    }
                    sb.Append("</ul>");
                }
            }
            return sb.ToString();
        }

        #endregion
    }
}